/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JF_AddUsuario.java
 *
 * Created on 09-mar-2010, 7:01:42
 */

package cliente.presentacion.administrador.gestionusuario;



import cliente.negocio.Controlador_Gestion_Usuarios;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import server.dominio.Centro;

/**
 *
 * @author greenbite
 */
public class JF_AddUsuario extends javax.swing.JFrame implements iJF_AddUsuario{
    
     private Controlador_Gestion_Usuarios C_Admin;
    /** Creates new form JF_AddUsuario */
    public JF_AddUsuario(Controlador_Gestion_Usuarios C_Admin) {
        initComponents();
        iniciaragnos();
        this.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/4,(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/6);
        this.C_Admin= C_Admin;
        this.iniciarCentros();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox5 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox6 = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jLabel15 = new javax.swing.JLabel();
        jTextField_password = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        jTextField_login = new javax.swing.JTextField();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jTextField_mail = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        jTextField_nombre = new javax.swing.JTextField();
        jTextField_apellidos = new javax.swing.JTextField();
        jTextField_NIF = new javax.swing.JTextField();
        jTextField_domicilio = new javax.swing.JTextField();
        jTextField_tel_fijo = new javax.swing.JTextField();
        jTextField_telefono_movil = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos Medico"));

        jLabel2.setText("Especialidad");

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cabecera", "Pediatría", "Urología", "Cirugía", "Neurología", "Ginecología", "Digestivo", "Oftalmología" }));
        jComboBox5.setEnabled(false);

        jLabel3.setText("Centro");

        jComboBox6.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel3)
                        .add(201, 201, 201))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jComboBox5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jComboBox6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 227, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jComboBox5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jLabel2))
                    .add(jComboBox6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jButton4.setText("Añadir Usuario");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/1268112229_list-add-user.png"))); // NOI18N

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Datos de usuario"));
        jLayeredPane2.setEnabled(false);

        jLabel15.setText("Pass:");
        jLabel15.setBounds(20, 70, 40, 16);
        jLayeredPane2.add(jLabel15, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_password.setBounds(70, 70, 170, 20);
        jLayeredPane2.add(jTextField_password, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel16.setText("Rol:");
        jLabel16.setBounds(20, 100, 40, 20);
        jLayeredPane2.add(jLabel16, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Citador", "Medico" }));
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });
        jComboBox4.setBounds(70, 100, 180, 22);
        jLayeredPane2.add(jComboBox4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel19.setText("Login:");
        jLabel19.setBounds(20, 40, 40, 16);
        jLayeredPane2.add(jLabel19, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_login.setBounds(70, 40, 170, 20);
        jLayeredPane2.add(jTextField_login, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Datos personales"));
        jLayeredPane3.setEnabled(false);

        jLabel17.setText("Fecha de nacimiento:");
        jLabel17.setBounds(20, 200, 130, 16);
        jLayeredPane3.add(jLabel17, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel18.setText("Nombre:");
        jLabel18.setBounds(20, 40, 60, 16);
        jLayeredPane3.add(jLabel18, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel20.setText("Apellidos:");
        jLabel20.setBounds(20, 70, 70, 16);
        jLayeredPane3.add(jLabel20, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel21.setText("NIF:");
        jLabel21.setBounds(270, 40, 40, 16);
        jLayeredPane3.add(jLabel21, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel22.setText("Domicilio:");
        jLabel22.setBounds(20, 100, 70, 16);
        jLayeredPane3.add(jLabel22, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel23.setText("Telf. Fijo:");
        jLabel23.setBounds(20, 130, 70, 16);
        jLayeredPane3.add(jLabel23, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel24.setText("Telf. Movil:");
        jLabel24.setBounds(230, 130, 70, 16);
        jLayeredPane3.add(jLabel24, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_mail.setBounds(100, 160, 330, 20);
        jLayeredPane3.add(jTextField_mail, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel26.setText("Email:");
        jLabel26.setBounds(20, 160, 60, 16);
        jLayeredPane3.add(jLabel26, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_nombre.setBounds(100, 40, 140, 20);
        jLayeredPane3.add(jTextField_nombre, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_apellidos.setBounds(100, 70, 330, 20);
        jLayeredPane3.add(jTextField_apellidos, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_NIF.setBounds(310, 40, 120, 20);
        jLayeredPane3.add(jTextField_NIF, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_domicilio.setBounds(100, 100, 330, 20);
        jLayeredPane3.add(jTextField_domicilio, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_tel_fijo.setBounds(100, 130, 110, 20);
        jLayeredPane3.add(jTextField_tel_fijo, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jTextField_telefono_movil.setBounds(310, 130, 120, 20);
        jLayeredPane3.add(jTextField_telefono_movil, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        jComboBox1.setBounds(160, 200, 70, 22);
        jLayeredPane3.add(jComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        jComboBox2.setBounds(230, 200, 120, 22);
        jLayeredPane3.add(jComboBox2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox3.setBounds(350, 200, 64, 22);
        jLayeredPane3.add(jComboBox3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(32, 32, 32)
                                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 161, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jButton1)))
                        .add(18, 18, 18)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 141, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(196, 196, 196)
                    .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLayeredPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 234, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
                    .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(17, 17, 17)
                    .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 143, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(400, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if(camposobligatorios()){
            String mes, dia;
            if(jComboBox2.getSelectedIndex()<9){
                mes= "0"+(jComboBox2.getSelectedIndex()+1);
            }else{
                mes= ""+(jComboBox2.getSelectedIndex()+1);
            }
            if(jComboBox1.getSelectedIndex()<9){
                dia= "0"+(jComboBox1.getSelectedIndex()+1);
            }else{
                dia= ""+(jComboBox1.getSelectedIndex()+1);
            }
            String fechanacimiento= jComboBox3.getSelectedItem().toString() + "-" + mes + "-"+dia;
            String tipomedico= "";
            String centro= "";

            if(jComboBox5.isEnabled()){
                tipomedico= ""+(jComboBox5.getSelectedIndex()+1);
                centro= ""+(jComboBox6.getSelectedIndex()+1);
                String [] informacion= {jTextField_login.getText(), jTextField_password.getText(), jTextField_NIF.getText(), jTextField_nombre.getText(), jTextField_apellidos.getText(), jTextField_domicilio.getText(), jTextField_mail.getText(), jTextField_tel_fijo.getText(), jTextField_telefono_movil.getText(), fechanacimiento, ""+(jComboBox4.getSelectedIndex()+1), tipomedico, centro};
                C_Admin.nuevousuario(informacion);
            }else{
                String [] informacion= {jTextField_login.getText(), jTextField_password.getText(), jTextField_NIF.getText(), jTextField_nombre.getText(), jTextField_apellidos.getText(), jTextField_domicilio.getText(), jTextField_mail.getText(), jTextField_tel_fijo.getText(), jTextField_telefono_movil.getText(), fechanacimiento, ""+(jComboBox4.getSelectedIndex()+1)};
               C_Admin.nuevousuario(informacion);
            }
        }else{
            JOptionPane.showMessageDialog(null, "Introduzca todos los datos obligatorios", "Error al introducir datos", JOptionPane.ERROR_MESSAGE);
        }

}//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed
        if(jComboBox4.getSelectedIndex()==2){
            jComboBox5.setEnabled(true);
            jComboBox6.setEnabled(true);
        }else{
            jComboBox5.setEnabled(false);
            jComboBox6.setEnabled(false);
        }
    }//GEN-LAST:event_jComboBox4ActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField_NIF;
    private javax.swing.JTextField jTextField_apellidos;
    private javax.swing.JTextField jTextField_domicilio;
    private javax.swing.JTextField jTextField_login;
    private javax.swing.JTextField jTextField_mail;
    private javax.swing.JTextField jTextField_nombre;
    private javax.swing.JTextField jTextField_password;
    private javax.swing.JTextField jTextField_tel_fijo;
    private javax.swing.JTextField jTextField_telefono_movil;
    // End of variables declaration//GEN-END:variables

    private void iniciaragnos() {
        String [] datos= new String[160];
        for(int i=0; i<160; i++){
            datos[i]= ""+ (1900 +i);
        }
        jComboBox3.setModel(new DefaultComboBoxModel(datos));
    }

    public void mostrar(boolean opcion) {
        this.setVisible(opcion);
    }

    private boolean camposobligatorios() {
        boolean solucion= true;
        solucion= solucion && jTextField_NIF.getText().length()>0 && jTextField_nombre.getText().length()>0;
        solucion= solucion && jTextField_apellidos.getText().length()>0 && jTextField_tel_fijo.getText().length()>0;
        solucion= solucion && jTextField_login.getText().length()>0 && jTextField_password.getText().length()>0;
        return solucion;

    }

    public void error(int i) {
        switch(i){
            case 1:
                JOptionPane.showMessageDialog(null, "El dni del Usuario o su login Ya existe, por favor compruebe sus datos", "Error de Datos Usuario", JOptionPane.ERROR_MESSAGE);
                break;
            case 2:
                JOptionPane.showMessageDialog(null, "El dni del Usuario o su login Ya existe, por favor compruebe sus datos", "Error de Datos Medico", JOptionPane.ERROR_MESSAGE);
                break;
        }
    }

    public void cerrar() {
        this.dispose();
    }

    private void iniciarCentros() {
        ArrayList<Centro> centros= C_Admin.getListacentros();
        String [] datos;
        if(centros== null){
            //Si todavia no se han recibido, mostrar 1
            datos= new String[1];
            datos[0]= "1 General no seque";
        }else{
            datos= new String[centros.size()];
            for(int i=0; i<centros.size(); i++){
                datos[i]= centros.get(i).getNombre() + "  " +centros.get(i).getLocalidad();
            }
        jComboBox6.setModel(new DefaultComboBoxModel(datos));
        }

    }

}
